package com.zt.dev.standardManage.service.impl;

import com.zt.dev.standardManage.entity.enmu.CategoryEnum;
import com.zt.dev.standardManage.entity.vo.Category;
import com.zt.dev.standardManage.entity.vo.Model;
import com.zt.dev.standardManage.entity.vo.ResultVO;
import com.zt.dev.standardManage.entity.vo.Standards;
import com.zt.dev.standardManage.mapper.ModelsMapper;
import com.zt.dev.standardManage.mapper.StandardMapper;
import com.zt.dev.standardManage.service.ModelsService;
import com.zt.dev.standardManage.utils.DateUtil;
import com.zt.dev.standardManage.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ModelsServiceImpl implements ModelsService {

    @Autowired
    private ModelsMapper modelsMapper;
    @Autowired
    private StandardMapper standardMapper;

    /**
     * 新建评估模板
     * @param model
     * @return
     */
    @Override
    public int insertModels(Model model) {
        //判断模板类别
        int node = CategoryEnum.getCode(model.getCategory());
        model.setCategoryNode(node);
        //创建时间
        String createTime = DateUtil.getStringDate();
        model.setCreateTime(createTime);
        model.setFrequency(0);
        int result = modelsMapper.insertModels(model);
        if(result > 0){
            //生成模板成功
            if(model.getFlag().equals("true")){
                //生成准则
                Standards standards = new Standards();
                standards.setStandardName(model.getModelName());
                standards.setTarget(model.getTarget());
                standards.setMethod(model.getMethod());
                standards.setContext(model.getContext());
                standards.setCategoryNode(node);
                standards.setFrequency(0);
                standards.setCreater(model.getCreater());
                standards.setCreateTime(createTime);
                int size = standardMapper.insertStandard(standards);
                if(size > 0){
                    result++;
                }else{
                    result--;
                }
            }
        }
        return result;
    }

    /**
     * 分页查询模板列表
     * @return
     */
    @Override
    public PageBean<Model> getAllModels(int currentPage,int pageSize,String modelName) {
        //获得总数
        int count = modelsMapper.getCount(modelName);
        PageBean pageBean = PageBean.pageCommon(count, currentPage, pageSize);
        //起始 结束 页
        int start = (currentPage-1)*pageBean.getPageSize();
        int end = pageBean.getPageSize();
        //封装每页显示的数据
        List<Model> modelList = modelsMapper.getAllModels(start,end,modelName);
        for(Model model : modelList){
            model.setCategory(CategoryEnum.getType(model.getCategoryNode()));
        }
        pageBean.setLists(modelList);
        return pageBean;
    }

    /**
     * 删除评估模板
     * @param ids
     * @return
     */
    @Override
    public int deleteModels(Integer[] ids) {
        int result = 0;
        for(Integer id : ids){
            result = modelsMapper.deleteModels(id);
            result++;
        }
        return result;
    }

    /**
     * 修改模板
     * @param model
     * @return
     */
    @Override
    public int updateModel(Model model) {
        model.setCategoryNode(CategoryEnum.getCode(model.getCategory()));
        return modelsMapper.updateModel(model);
    }

    /**
     * 依据模板，生成准则
     * @param standards,id
     * @param id
     * @return
     */
    @Override
    public int addStandard(Standards standards, Integer id) {
        //判断准则类别
        standards.setCategoryNode(CategoryEnum.getCode(standards.getCategory()));
        //创建时间
        String createTime = DateUtil.getStringDate();
        standards.setCreateTime(createTime);
        //默认生成准则调用次数为0
        standards.setFrequency(0);
        //生成准则
        int result = standardMapper.insertStandard(standards);
        //成功生成准则
        if(result > 0){
            //修改模板调用次数
            Model model = modelsMapper.getModelById(id);
            int frequency = model.getFrequency();
            frequency = frequency+1;
            model.setFrequency(frequency);
            int size = modelsMapper.updateModel(model);
            if(size > 0){
                result++;
            }else{
                result--;
            }
        }
        return result;
    }

    /**
     * 统计当前月各类别量
     * @return
     */
    @Override
    public Map<String,Object> getModelByMon(String createTime) {
        Map<String,Object> resultMap = new HashMap<>();
        //结果集
        List<ResultVO> resultVOList = new ArrayList<>();
        String time = null;
        List<Category> categoryList = standardMapper.getAllType();   //获取类别
        for(int i = 0 ;i < categoryList.size();i++){
            ResultVO resultVO = modelsMapper.getModelByMon(i,createTime);
            if(resultVO != null){
                time = resultVO.getTime();
                resultVO.setCategory(categoryList.get(i).getName());
                resultVOList.add(resultVO);
            }else{
                ResultVO resultVO1 = new ResultVO();
                time = DateUtil.getStringDate();
                if(createTime != null) {
                    time = time.substring(0,7);  //截取月份
                }else{
                    int year = Integer.parseInt(time.substring(0,4));
                    int month = Integer.parseInt(time.substring(5,7));
                    String time1 = time.substring(5,7);
                    if(time1.indexOf('0')== 0){
                        time1 = time1.substring(1,2);
                        month = Integer.parseInt(time1);
                        if (month == 1) {
                            month = 12;
                        } else {
                            month = month - 1;
                        }
                    }else{
                        month = month - 1;
                    }

                    time = year + "-" + month;
                }
                resultVO1.setCategory(categoryList.get(i).getName());
                resultVO1.setNumber(0);
                resultVOList.add(resultVO1);
            }
        }
        String title = time+"各类别模板生成数量比";
        resultMap.put("title",title);
        resultMap.put("list",resultVOList);
        return resultMap;
    }


}
